<template>
	<div>
		<el-form :disabled="isDisabled" class="dialog-layout" ref="userForm" :rules="rules" :model="state.ruleForm" size="default" label-width="100px">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" prop>
					<el-form-item label="模型名称" prop="modelName">
						<el-input v-model="state.ruleForm.modelName" placeholder="请输入风控模型名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="状态" required>
						<el-switch v-model="state.ruleForm.status" :active-value="10" :inactive-value="0" active-text="开启" inactive-text="禁用" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<div class="dialog-btn">
			<el-button @click="onCancel" size="default">取 消</el-button>
			<el-button :disabled="isDisabled" type="primary" @click="onSubmit(userForm)" size="default">确 认</el-button>
		</div>
	</div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { RiskModelCreate, RiskModelUpdate } from '/@/api/model/index';
import { FormInstance, ElMessage } from 'element-plus';
// 定义父组件传过来的值
const props = defineProps({
	editItem: {
		type: Object,
		default: () => {},
	},
	isDisabled: {
		type: Boolean,
		default: false,
	},
});
// 定义子组件向父组件传值/事件
const emit = defineEmits(['close']);

// 定义变量内容
const state = reactive({
	ruleForm: {
		modelName: '', // 名称
		status: 10,
	} as any,
	num: 0,
});
onMounted(() => {
	getTableData();
});
// 初始化列表
const getTableData = async () => {
	if (Object.keys(props.editItem).length) {
		for (const key in state.ruleForm) {
			if (props.editItem[key]) state.ruleForm[key] = props.editItem[key];
			state.ruleForm.status = Number(props.editItem.status);
		}
	}
};

// 关闭弹窗
const onCancel = () => {
	emit('close');
};

const rules = reactive({
	modelName: [{ required: true, message: '请输入风控模型名称', trigger: ['blur', 'change'] }],
});
const userForm = ref<FormInstance>();
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate(async (valid) => {
		if (valid) {
			const params = {
				...state.ruleForm,
			};
			if (Object.keys(props.editItem).length) {
				await RiskModelUpdate(params, props.editItem.id);
			} else {
				await RiskModelCreate(params);
			}
			ElMessage.success(Object.keys(props.editItem).length ? '修改成功' : '新增成功');
			onCancel();
		}
	});
};
</script>


